/**
 * @(#) SchoolExpProxy.as
 *
 * @date		2010. 11. 01.
 * @version		2.0
 * @author		Shin, Yunwoo
 * @client		Service Ace
  * @module		notice (공지사항 관리)
 * @description	공지사항 상세 내용 - Proxy
 *
 */
package com.skt.common_hr.schoolexp_nos.model
{
	import com.skt.common_hr.common.Variables;
	import com.skt.common_hr.common.puremvc.CommonProxy;
	import com.skt.common_hr.schoolexp_nos.model.vo.SchoolExpFileUpLoadVO;
	import com.skt.common_hr.schoolexp_nos.model.vo.SchoolExpVO;
	import com.wcl.utils.DateUtil;
	import com.wcl.utils.StringUtil;
	
	import flash.net.URLVariables;
	
	import mx.collections.ArrayCollection;
	import mx.rpc.events.FaultEvent;
	import mx.rpc.events.ResultEvent;

	public class SchoolExpProxy extends CommonProxy
	{
		public static const NAME:String = "SchoolExpProxy";

		public static const LOAD_SCHOOLEXP_COMPLETE:String = "loadSchoolExpComplete";
		public static const LOAD_SCHOOLEXP_FAILED:String = "loadSchoolExpFailed";
		public static const SAVE_SCHOOLEXP_COMPLETE:String = "saveSchoolExpComplete";
		public static const SAVE_SCHOOLEXP_FAILED:String = "saveSchoolExpFailed";
		public static const DELETE_SCHOOLEXP_COMPLETE:String = "delSchoolExpComplete";
		public static const DELETE_SCHOOLEXP_FAILED:String = "delSchoolExpFailed";
		public static const LOAD_PRINTDATA_COMPLETE:String = "loadPrintDataComplete";
		public static const LOAD_PRINTDATA_FAILED:String = "loadPrintDataFailed";

		public var retKey:String = "";

		public var schoolExpList:ArrayCollection;
		public var schoolExpFile:SchoolExpFileUpLoadVO;

		public var urlParam:URLVariables;

		/**
		 * 생성자
		 *
		 * @param
		 * @return void
		 */
		public function SchoolExpProxy(data:Object=null)
		{
			super(NAME, data);
		}


		/**
		 * data load를 수행
		 * 		- DataLoader 를 생성하고 Import Parameter 객체를 생성, RFC를 호출
		 *
		 * @param pernr 사번
		 * @return void
		 */
		public function load(key:String):void
		{
			var param:Object = new Object();
			param["I_WEL_KEY01"] = key;

			loadData("Z_HR_0153", param, result, fault);
		}

		public function save(key:String, pernr:String, type:String, table:Array, fileVo:SchoolExpFileUpLoadVO):void
		{
			var params:Object = new Object();
			params["I_GWAREKEY"] = key;
			params["I_PERNR"] = Variables.myPernr;
			// key가  없으면 신규, 있으면 수정
			params["I_PROCGB"] = (StringUtil.NVL(key)=="") ? "C" : "M";		// c:신규, m:수정, d:삭제
			params["I_SLART"] = type;
			
			// 첨부파일 
			if(fileVo.I_FILE_NAME!=null) {
				params["I_FILE_NAME"] = fileVo.I_FILE_NAME;
				params["I_FILE_EXT"] = fileVo.I_FILE_EXT;
				params["I_FILE_URL"] = fileVo.I_FILE_URL;
				params["I_FILE_SIZE"] = fileVo.I_FILE_SIZE.toString();
			}

			var tables:Object = new Object();
			tables["ITAB"] = table;

			saveData("Z_HR_0151", params, tables, resultSave, faultSave);
		}

		public function deleteData(key:String, pernr:String):void
		{
			var params:Object = new Object();
			params["I_GWAREKEY"] = key;
			params["I_PROCGB"] = "D";		// c:신규, m:수정, d:삭제

			var tables:Object = new Object();
			tables["ITAB"] = new Array();

			saveData("Z_HR_0151", params, tables, resultDelete, faultDelete);
		}

		public function printData(key:String):void
		{
			var param:Object = new Object();
			param["I_WEL_KEY01"] = key;

			loadData("Z_HR_0153", param, resultPrint, faultPrint); 
		}

		///////////////////////////////////////////////////////////////////////////////////////////////////
		//
		//	Event Listeners
		//
		///////////////////////////////////////////////////////////////////////////////////////////////////


		/**
		 * 서버에서 정상적으로 리턴된 결과를 처리
		 *
		 * @param
		 * @return void
		 */
		public function result(event:ResultEvent):void
		{
			if(retCode==0) {
				schoolExpList = parseDetail(event.result.OTAB);
				
				schoolExpFile = new SchoolExpFileUpLoadVO();
				schoolExpFile.I_FILE_EXT = event.result.E_FILE_EXT;
				schoolExpFile.I_FILE_NAME = event.result.E_FILE_NAME;
				schoolExpFile.I_FILE_SIZE = event.result.E_FILE_SIZE;
				schoolExpFile.I_FILE_URL = event.result.E_FILE_URL;
				
				if(event.result.E_FILE_NAME == '') {
					schoolExpFile.I_FILE_EXT = '';
					schoolExpFile.I_FILE_SIZE = 0;
					schoolExpFile.I_FILE_URL = '';					
				}
				
				facade.sendNotification(LOAD_SCHOOLEXP_COMPLETE, this);
			}
			else {
				facade.sendNotification(LOAD_SCHOOLEXP_FAILED, this);
			}
		}

		/**
		 * 서버에서 정상적으로 리턴된 결과를 처리
		 *
		 * @param
		 * @return void
		 */
		public function resultPrint(event:ResultEvent):void
		{
			if(retCode==0) {
				schoolExpList = parseDetail(event.result.OTAB);
				urlParam = buildURLParams();
				facade.sendNotification(LOAD_PRINTDATA_COMPLETE, this);
			}
			else {
				facade.sendNotification(LOAD_PRINTDATA_FAILED, this);
			}
		}

		private function buildURLParams():URLVariables
		{
			var total:Number = 0;
			var params:URLVariables = new URLVariables();
			if(schoolExpList.length>=1) {
				var header:SchoolExpVO = schoolExpList.getItemAt(0) as SchoolExpVO;

				params["PERNR"] = Variables.myPernr;
				params["ENAME"] = Variables.myName;
				params["ORGTX"] = Variables.myOrg;
				params["TEL_OFFICE"] = "";
				params["TEL"] 	= Variables.myTel;;

				params["BEGDA"] = header.begda;
				params["DETAIL_COUNT"] = schoolExpList.length;

				params["STEXT"] = header.STEXT;


				for(var i:int=0; i<schoolExpList.length; i++) {
					var vo:SchoolExpVO = schoolExpList.getItemAt(i) as SchoolExpVO;
					params["FCNAM"+i.toString()] = vo.FCNAM;
					params["REGNO"+i.toString()] = vo.regno;
					params["ZYEAR"+i.toString()] = vo.ZYEAR;
					params["ZSLART"+i.toString()] = vo.ZSLART;
					params["ZGRADE"+i.toString()] = vo.ZGRADE;
					params["ZCOUMS"+i.toString()] = vo.ZCOUMS;
					params["TOTPY"+i.toString()] = vo.totpy;
					if(vo.ZFDATE!=null && vo.ZTDATE!=null)  {
						params["TERM"+i.toString()] = vo.ZFDATE.fullYear + "년 " + (vo.ZFDATE.month+1) + "월 ~ " +vo.ZTDATE.fullYear + "년 " + (vo.ZTDATE.month+1) + "월";
					}
					else if(vo.ZTDATE==null && vo.ZFDATE==null) {
						params["TERM"+i.toString()] = " ";						
					}
					else if(vo.ZTDATE==null) {
						params["TERM"+i.toString()] = vo.ZFDATE.fullYear + "년 " + (vo.ZFDATE.month+1) + "월";						
					}					
					else {
						params["TERM"+i.toString()] = " ";
					}
				}
			}
			return params
		}
		/**
		 * 서버에서 정상적으로 리턴된 결과를 처리
		 *
		 * @param
		 * @return void
		 */
		public function resultSave(event:ResultEvent):void
		{
			if(retCode==0) {
				retKey = event.result.O_GWAREKEY;
				facade.sendNotification(SAVE_SCHOOLEXP_COMPLETE, this);
			}
			else {
				facade.sendNotification(SAVE_SCHOOLEXP_FAILED, this);
			}
		}

		public function resultDelete(event:ResultEvent):void
		{
			if(retCode==0) {
				facade.sendNotification(DELETE_SCHOOLEXP_COMPLETE, this);
			}
			else {
				facade.sendNotification(DELETE_SCHOOLEXP_FAILED, this);
			}
		}
		/**
		 * 서버에서 오류가 발생했을 처리
		 *
		 * @param
		 * @return void
		 */
		private function fault(event:FaultEvent):void
		{
			facade.sendNotification(LOAD_SCHOOLEXP_FAILED, this);
		}

		/**
		 * 서버에서 오류가 발생했을 처리
		 *
		 * @param
		 * @return void
		 */
		private function faultPrint(event:FaultEvent):void
		{
			facade.sendNotification(LOAD_PRINTDATA_FAILED, this);
		}

		/**
		 * 서버에서 오류가 발생했을 처리
		 *
		 * @param
		 * @return void
		 */
		private function faultSave(event:FaultEvent):void
		{
			facade.sendNotification(SAVE_SCHOOLEXP_FAILED, this);
		}

		/**
		 * 서버에서 오류가 발생했을 처리
		 *
		 * @param
		 * @return void
		 */
		private function faultDelete(event:FaultEvent):void
		{
			facade.sendNotification(DELETE_SCHOOLEXP_FAILED, this);
		}



		///////////////////////////////////////////////////////////////////////////////////////////////////
		//
		//	Data Parsing functions
		//
		///////////////////////////////////////////////////////////////////////////////////////////////////
		/**
		 * 서버에서 전달된 result 객체를 분석하여 Value Object를 생성
		 *
		 * @param
		 * @return 공지사항 Value Object
		 */
		 private function parseDetail(src:ArrayCollection):ArrayCollection
		{
			var list:ArrayCollection = new ArrayCollection;

			for(var i:int = 0; i < src.length; i++)
			{
				var vo:SchoolExpVO = new SchoolExpVO();

				vo.WEL_KEY01	= src[i].WEL_KEY01;							//
				vo.PERNR		= src[i].PERNR;
				vo.SLART		= src[i].SLART;
				vo.STEXT		= src[i].STEXT;
				vo.BEGDA		= DateUtil.stringToDateTime(src[i].BEGDA);	//
				//vo.ZPAYTX		= DateUtil.stringToDateTime(src[i].ZPAYTX);	//지급일
				vo.ZPAYTX		= src[i].ZPAYTX;	//지급일

				vo.WSTATUS		= src[i].WSTATUS;							//
				vo.WTEXT		= src[i].WTEXT;							//

				vo.FAMSA		= src[i].FAMSA;								//가족관계
				vo.ZTEXT		= src[i].ZTEXT;								//가족관계
				vo.FCNAM		= src[i].FCNAM;								//가족이름
				vo.REGNO		= src[i].REGNO;								//가족이름
				vo.ZOLD			= src[i].ZOLD;								//가족이름
				vo.ZYEAR		= src[i].ZYEAR;								//가족이름
				vo.ZGRADE		= src[i].ZGRADE;							//학년
				vo.ZCOUMS		= src[i].ZCOUMS;							//학년

				vo.TOTPY		= src[i].TOTPY * 100;								//신청금액
				vo.TOTPY2		= src[i].TOTPY2 * 100;							//지급금액
				vo.ZSLART		= src[i].ZSLART;							//학교명

				vo.ZFDATE		= DateUtil.stringToDateTime(src[i].ZFDATE);	//
				vo.ZTDATE		= DateUtil.stringToDateTime(src[i].ZTDATE);	//
				list.addItem(vo);
			}

			return list;
		}
	}
}